package com.asia5b.a5mall.shop.activity

import androidx.databinding.DataBindingUtil
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.event.ShopCategorySelectEvent
import com.asia5b.a5mall.middleware.utils.*
import com.asia5b.a5mall.shop.R
import com.asia5b.a5mall.shop.databinding.ActivityShopsearchgoodsBinding
import com.asia5b.a5mall.shop.fragment.ShopAllFragment
import kotlinx.android.synthetic.main.activity_shopsearchgoods.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   店铺内搜索商品展示列表
 */
@Route(path = ActCons.ShopSearchGoodsActivity)
class ShopSearchGoodsActivity : SimpleActivity<SimpleViewModel, ActivityShopsearchgoodsBinding>() {
    override val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java

    override fun getDataBinding(): ActivityShopsearchgoodsBinding {
        shopId = intent.getIntExtra("shopId", 0)
        if (intent.hasExtra("category")) {
            category = intent.getStringExtra("category")!!
        }
        if (intent.hasExtra("keyword")) {
            keyword = intent.getStringExtra("keyword")!!
        }
        return ActivityShopsearchgoodsBinding.inflate(layoutInflater)
    }

    override var sIsDark = true

    /**
     * @Name  shopId
     * @Type  Int
     * @Note  店铺id（自动注入）
     */
    var shopId: Int = 0

    /**
     * @Name category
     * @Type String
     * @Note 店铺分类id
     */
    var category: String = ""

    /**
     * @Name keyword
     * @Type String
     * @Note 搜索关键字
     */
    var keyword: String = ""


    /**
     * @Name  fragment
     * @Type  ShopAllFragmentAllView
     * @Note  店铺商品Fragment
     */
    private val fragment by lazy {
        val bundle = Bundle()
        bundle.putInt("shopId", shopId)
        bundle.putString("category", category)
        bundle.putString("keyword", keyword)
        sNewStanceFragment(mActivity, ShopAllFragment::class.java, bundle)
    }

    /**
     * @Name  binding
     * @Type  ShopSearchGoodsLayBinding
     * @Note  视图绑定
     */
    private val binding by lazy {
        DataBindingUtil.bind<ActivityShopsearchgoodsBinding>(layoutInflater.inflate(R.layout.activity_shopsearchgoods, null))!!
    }


    override fun initView() {
        supportFragmentManager.beginTransaction().add(R.id.fl_shop_container, fragment, "detail").commit()

        if (keyword.isNotEmpty()) {
            et_shop_keyword.setText(keyword)
        }
    }

    override fun initEvent() {

        et_shop_keyword.setOnEditorActionListener { v, actionId, event ->
            keyword = et_shop_keyword.text.toString()
            fragment.searchKeyword(keyword)
            InputMethodUtils.closeKeyBoard(mActivity)
            return@setOnEditorActionListener true
        }
    }

    override fun initData() {

    }

    override val isRegisterEventBus: Boolean
        get() = true

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onShopCategorySelectEvent(event: ShopCategorySelectEvent) {
        fragment.category = event.category
        fragment.getDatas(1)
    }

}